package main

import (
	"os"
	"path"
	"net/url"
)

type simple struct {
	PACKAGE_BASE string
	URL  string
	Name string
	Prefix string
}

func (s simple) PackagesCacheDir() string {
	return path.Join("cache", s.Name, "packages")
}

func (s simple) SimplesCacheDir() string {
	return path.Join("cache", s.Name, "simples")
}

func (s simple) JoinWithBase(path string) string {
	target_url, err :=url.JoinPath(s.PACKAGE_BASE, path)
	if err != nil {
		panic(err)
	}
	return target_url
}


type global struct {
	Offline      bool
	Simples      []simple
	CurrentIndex int
}

var Global global = global{
	Offline: true,
	Simples: []simple{
		simple{
			PACKAGE_BASE: "http://mirrors.cloud.tencent.com/pypi/",
			URL:  "http://mirrors.cloud.tencent.com/pypi/simple/",
			Name: "tencent",
			Prefix: "/packages",
		},
		simple{
			URL:  "https://download.pytorch.org/whl/cu124",
			Name: "torch",
			Prefix: "/whl",
			PACKAGE_BASE: "https://download.pytorch.org/",
		},
	},
	CurrentIndex: 1,
}

func InitGlobal() {
	var err error
	mode := os.FileMode(0755)

	for _, s := range Global.Simples {
		err = os.MkdirAll(s.PackagesCacheDir(), mode)
		if err != nil {
			panic(err)
		}
		err = os.MkdirAll(s.SimplesCacheDir(), mode)
		if err != nil {
			panic(err)
		}
	}
}
